<?php


class imfLib
{

	private static $instance;
 
	private function __construct() {}
	private function __clone() {}
 
	public static function getInstance() {
		if (self::$instance === null) 
		{
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	function getThumb($filename, $newwidth, $newheight, $thumbfilename, $quality)
	{

 		list ($width,$height,$type) = $this->getImageSize($filename);
		$thumbfilename = $thumbfilename.".".$this->getTypeString(/*$type*/2);

                $newheight = min($newheight,$newwidth);
                $newwidth = $newheight;
                /*
		if($height <= $newheight && $width <= $newwidth){
			$newheight = $height;
			$newwidth  = $width;
		}
		else if($height > $width){
			$newheight = $newheight;
			$newwidth  = round($width / ($height/$newheight));
		}			
		else 
		{
			$newwidth  = $newwidth;
			$newheight = round($height / ($width/$newwidth));
		}*/
		
		if(file_exists($thumbfilename))	
			return array($thumbfilename, $newwidth, $newheight);
		
		return array($this->createThumb($filename, $width, $height, $type, $newwidth, $newheight, $thumbfilename, $quality), $newwidth, $newheight);	
	}
	
	function createThumb($filename, $width, $height, $type, $newwidth, $newheight, $thumbfilename, $quality)
	{

            $rectWidth = $width;
            $rectHeight = $height;
            $src_x = 0;
            $src_y = 0;
            
            if($width > $height)
            {
                $rectWidth = $height;
                $rectHeight = $height;
                $src_x = ($width-$height)/2;
            } elseif($width < $height)
            {
                $rectWidth = $width;
                $rectHeight = $width;
                $src_y = ($height-$width)/2;
            }
		if($context = $this->loadImageFromFile($filename,$type))
		{
			$context2 = ImageCreateTrueColor($newwidth, $newheight);
			ImageCopyResampled($context2, $context, 0, 0, $src_x, $src_y, $newwidth, $newheight, $rectWidth, $rectHeight);

			if($this->saveImageToFile($context2, $thumbfilename , /*$type*/ 2, $quality))
				return $thumbfilename;
			else
				return false;			
		}
		
		return false;		
	}
	
	function getTypeString($type)
	{
		switch ($type) 
		{
			case 1 :
			{
				return 'gif';
				break;
			}

			case 2 :
			{
				return 'jpg';
				break;
			}

			case 3 :
			{
				return 'png';
				break;
			}

			default: return null;
		}
	}
	
	function getImageSize($filename)
	{
		return GetImageSize($filename);
	}
	
	function loadImageFromFile($filename, $type)
	{
		$imagetypes = ImageTypes();

		switch ($type) 
		{
			case 1 :
			{
				if ($imagetypes & IMG_GIF)
					return ImageCreateFromGIF($filename);
				else 
					return null;
				break;
			}

			case 2 :
			{
				if ($imagetypes & IMG_JPEG)
					return ImageCreateFromJPEG($filename);
				else
					return null;
				break;
			}

			case 3 :
			{
				if ($imagetypes & IMG_PNG)
					return ImageCreateFromPNG($filename);
				else 
					return null;
				break;
			}

			default: return null;
		}
	}
	
	function saveImageToFile($context, $filename, $type, $quality) {

		switch ($type) {
			case 1 :
				return ImageGIF($context, $filename);
			case 2 :
				return ImageJpeg($context, $filename, $quality);
			case 3 :
				return ImagePNG($context, $filename);
			default:
				return false;
		}
	}
}

?>